package com.lanou3g.code0516;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/5/16;
 * Otherwise, I don't know who created it either
 */

@WebServlet(name = "RegisterServlet",urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO 拿到用户名和密码

        // TODO 验证

        // TODO 根据验证结果 构件验证信息
        Random random = new Random();
        boolean registerSuccess = random.nextBoolean();

        String msg = "";
        if (registerSuccess){
            msg = "Register Success";
        } else {
            msg = "Register Failed";
        }
        // 把消息放到 request 域中 发送给jsp
        request.setAttribute("msg",msg);
        // 请求转发
        request.getRequestDispatcher("/user/msg.jsp")
                .forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
